home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / Level 0 Macintosh 01Jan95 / FloatingPoint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-30  |  4.5 KB  |  260 lines  |  [TEXT/KAHL]

  1. /* FloatingPoint.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    System Dependency Library for Building Portable Software               */
  5. /*    Macintosh Version                                                      */
  6. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  7. /*                                                                           */
  8. /*    This file is Public Domain; it may be used for any purpose whatsoever  */
  9. /*    without restriction.                                                   */
  10. /*                                                                           */
  11. /*    This package is distributed in the hope that it will be useful,        */
  12. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  13. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  14. /*                                                                           */
  15. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18.  
  19. #include "MiscInfo.h"
  20. #include "Debug.h"
  21. #include "Audit.h"
  22. #include "Definitions.h"
  23.  
  24. #ifdef THINK_C
  25.     #pragma options(pack_enums)
  26. #endif
  27. #if __powerc
  28.     #include <fp.h>
  29. #else
  30.     #include <SANE.h>
  31. #endif
  32. #ifdef THINK_C
  33.     #pragma options(!pack_enums)
  34. #endif
  35.  
  36. #include "FloatingPoint.h"
  37.  
  38.  
  39. #ifdef THINK_C
  40.     #if !__option(mc68881) && __option(native_fp)
  41.         /* native for no FPU = always 10-byte (no convertion necessary) */
  42.         #define x80tox96(X,Y) (*(Y) = *(X))
  43.         #define x96tox80(X,Y) (*(Y) = *(X))
  44.     #else
  45.         /* use normal definition */
  46.     #endif
  47. #else
  48.     #if __powerc
  49.         #define x80tox96(X,Y) (*(Y) = *(X))
  50.         #define x96tox80(X,Y) (*(Y) = *(X))
  51.     #endif
  52. #endif
  53.  
  54.  
  55. long double                XGETPI(void)
  56.     {
  57.         extended            A;
  58.         long double        B;
  59.  
  60. #if __powerc
  61.         A = pi;
  62. #else
  63.         A = pi();
  64. #endif
  65.         x80tox96(&A,&B);
  66.         return B;
  67.     }
  68.  
  69.  
  70. long double                XATAN(long double X)
  71.     {
  72.         extended            A;
  73.         long double        B;
  74.  
  75.         x96tox80(&X,&A);
  76.         A = atan(A);
  77.         x80tox96(&A,&B);
  78.         return B;
  79.     }
  80.  
  81.  
  82. long double                XCOS(long double X)
  83.     {
  84.         extended            A;
  85.         long double        B;
  86.  
  87.         x96tox80(&X,&A);
  88.         A = cos(A);
  89.         x80tox96(&A,&B);
  90.         return B;
  91.     }
  92.  
  93.  
  94. long double                XEXP(long double X)
  95.     {
  96.         extended            A;
  97.         long double        B;
  98.  
  99.         x96tox80(&X,&A);
  100.         A = exp(A);
  101.         x80tox96(&A,&B);
  102.         return B;
  103.     }
  104.  
  105.  
  106. long double                XFABS(long double X)
  107.     {
  108.         extended            A;
  109.         long double        B;
  110.  
  111.         x96tox80(&X,&A);
  112.         A = fabs(A);
  113.         x80tox96(&A,&B);
  114.         return B;
  115.     }
  116.  
  117.  
  118. long double                XLN(long double X)
  119.     {
  120.         extended            A;
  121.         long double        B;
  122.  
  123.         x96tox80(&X,&A);
  124.         A = log(A);
  125.         x80tox96(&A,&B);
  126.         return B;
  127.     }
  128.  
  129.  
  130. long double                XSIN(long double X)
  131.     {
  132.         extended            A;
  133.         long double        B;
  134.  
  135.         x96tox80(&X,&A);
  136.         A = sin(A);
  137.         x80tox96(&A,&B);
  138.         return B;
  139.     }
  140.  
  141.  
  142. long double                XSQRT(long double X)
  143.     {
  144.         extended            A;
  145.         long double        B;
  146.  
  147.         x96tox80(&X,&A);
  148.         A = sqrt(A);
  149.         x80tox96(&A,&B);
  150.         return B;
  151.     }
  152.  
  153.  
  154. long double                XTAN(long double X)
  155.     {
  156.         extended            A;
  157.         long double        B;
  158.  
  159.         x96tox80(&X,&A);
  160.         A = tan(A);
  161.         x80tox96(&A,&B);
  162.         return B;
  163.     }
  164.  
  165.  
  166. long double                XPOWER(long double X, long double Y)
  167.     {
  168.         extended            XX;
  169.         extended            YY;
  170.         extended            ZZ;
  171.         long double        Z;
  172.  
  173.         x96tox80(&X,&XX);
  174.         x96tox80(&Y,&YY);
  175. #if __powerc
  176.         ZZ = pow(XX,YY);
  177. #else
  178.         ZZ = power(XX,YY);
  179. #endif
  180.         x80tox96(&ZZ,&Z);
  181.         return Z;
  182.     }
  183.  
  184.  
  185. long double                XACOS(long double X)
  186.     {
  187. #if __powerc
  188.         return acos(X);
  189. #else
  190.         return 2 * XATAN(XSQRT((1 - X) / (1 + X)));
  191. #endif
  192.     }
  193.  
  194.  
  195. long double                XASIN(long double X)
  196.     {
  197. #if __powerc
  198.         return asin(X);
  199. #else
  200.         long double        Y;
  201.  
  202.         Y = XFABS(X);
  203.         if (Y > 0.5)
  204.             {
  205.                 Y = 1 - Y;
  206.                 Y = 2 * Y - Y * Y;
  207.             }
  208.          else
  209.             {
  210.                 Y = 1 - Y * Y;
  211.             }
  212.         return XATAN(X / XSQRT(Y));
  213. #endif
  214.     }
  215.  
  216.  
  217. MyBoolean                    ISNAN(long double X)
  218.     {
  219. #if __powerc
  220.         return !isfinite(X);
  221. #else
  222.         extended            XX;
  223.         numclass            Form;
  224.  
  225.         x96tox80(&X,&XX);
  226.         Form = classextended(XX);
  227.         return ((Form != NORMALNUM) && (Form != ZERONUM) && (Form != DENORMALNUM));
  228. #endif
  229.     }
  230.  
  231.  
  232. long double                XCEIL(long double X)
  233.     {
  234.         rounddir                OldRound;
  235.         extended                S;
  236.  
  237.         x96tox80(&X,&S);
  238.         OldRound = getround();
  239.         setround(UPWARD);
  240.         S = rint(S);
  241.         setround(OldRound);
  242.         x80tox96(&S,&X);
  243.         return X;
  244.     }
  245.  
  246.  
  247. long double                XFLOOR(long double X)
  248.     {
  249.         rounddir                OldRound;
  250.         extended                S;
  251.  
  252.         x96tox80(&X,&S);
  253.         OldRound = getround();
  254.         setround(DOWNWARD);
  255.         S = rint(S);
  256.         setround(OldRound);
  257.         x80tox96(&S,&X);
  258.         return X;
  259.     }
  260.